package org.swfer.UICExtend 
{
	import flash.text.TextField;
	
	/**
	 * print 过滤 和 简单重定向到文本
	 * @author swfer
	 */
	public class Logging 
	{
		public static const INFO:String = 'info';
		public static const ERROR:String = 'error';
		public static const WARN:String = 'warn';
		public static const DEBUG:String = 'debug';
		
		/** 打印过滤级别 **/
		public static var level:Array;
		
		/** 打印对象过滤级别 **/
		public static var only:Array;
		
		/** 打印重定向到文本 **/
		public static var redirect:TextField;
		private static var loggingDict:Object = { };
		private static var block:Boolean = true;
		
		private var tag:String;
		
		public function Logging(tag:String=null)
		{
			if (block) throw new Error('不允许构造。要获得对象请使用静态方法getLog');
			this.tag = tag;
		}
		
		/**
		 * 获得一个命名log单例对象
		 * @param	tag
		 * @return
		 */
		public static function getLog(tag:String='default'):Logging
		{
			block = false;
			loggingDict[tag] = loggingDict[tag] || new Logging(tag);
			block = true;
			return loggingDict[tag];
		}
		
		public static function removeLog(tag:String):void
		{
			delete loggingDict[tag];
		}
		
		public function info(...args):void
		{
			print(INFO,args);
		}
		
		public function error(...args):void
		{
			print(ERROR,args);
		}
		
		public function warn(...args):void
		{
			print(WARN,args);
		}
		
		public function debug(...args):void
		{
			print(DEBUG,args);
		}
		
		private function print(type:String, args:Array):void
		{
			if (level && level.indexOf(type) == -1) return;
			if (only && only.indexOf(tag) == -1) return;
			
			if (redirect) redirect.appendText("[" + type + "] " + args.join(' ')+'\n');
			else trace("[" + type + "] " + args.join(' '));
		}
	}
}